package com.yuyue.note02.controller.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器类实现类
 */
@Component
public class ProjectInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("--拦截方法前执行--");
        //false则跳过后续执行
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);

        System.out.println("请求报文的ContentType参数：" + request.getContentType());

        System.out.println("请求报文的CharacterEncoding参数：" + response.getCharacterEncoding());

        HandlerMethod handlerMethod = (HandlerMethod)handler;
        System.out.println("handler的方法名称:" + handlerMethod.getMethod().getName());

        System.out.println("--拦截方法后执行--");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
        System.out.println("--拦截方法完成执行--");

        System.out.println("错误信息：" + (ex ==null ? "" : ex.getMessage()));
    }
}
